﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DAL;
using Model;

namespace BLL
{
    public class BUser
    {

        private readonly DAL.DUser _dUser = new DUser();
        public  bool CheckUser(string userName, string password)
        {
            return _dUser.CheckUser(userName, password);
        }
         
        public UserInfo CheckUserWithReturn(string userName, string password)
        {
            return _dUser.CheckUserWithReturn(userName, password);
        }


        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public  int GetMaxId()
        {
            return _dUser.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string userName)
        {
            return _dUser.Exists(userName);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public  bool Add(Model.UserInfo model)
        {
            return _dUser.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public  bool Update(Model.UserInfo model)
        {
            return _dUser.Update(model);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        public bool ChangePassWordByUserName(string userName,string newPwd)
        {
            return _dUser.ChangePassWordByUserName(userName, newPwd);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool Delete(int id)
        {

            return _dUser.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public  bool DeleteList(string idlist)
        {
            return _dUser.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  Model.UserInfo GetModel(int id)
        {

            return _dUser.GetModel(id);
        }

        ///// <summary>
        ///// 得到一个对象实体，从缓存中
        ///// </summary>
        //public Model.UserInfo GetModelByCache(int id)
        //{

        //    string CacheKey = "tuserModel-" + id;
        //    object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
        //    if (objModel == null)
        //    {
        //        try
        //        {
        //            objModel = _dUser.GetModel(id);
        //            if (objModel != null)
        //            {
        //                int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
        //                Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
        //            }
        //        }
        //        catch { }
        //    }
        //    return (Model.UserInfo)objModel;
        //}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetList(string strWhere)
        {
            return _dUser.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.UserInfo> GetModelList(string strWhere)
        {
            DataSet ds = _dUser.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.UserInfo> DataTableToList(DataTable dt)
        {
            List<Model.UserInfo> modelList = new List<Model.UserInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.UserInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.UserInfo();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["UserName"] != null && dt.Rows[n]["UserName"].ToString() != "")
                    {
                        model.UserName = dt.Rows[n]["UserName"].ToString();
                    }
                    if (dt.Rows[n]["Password"] != null && dt.Rows[n]["Password"].ToString() != "")
                    {
                        model.Password = dt.Rows[n]["Password"].ToString();
                    }
                    if (dt.Rows[n]["Roleid"] != null && dt.Rows[n]["Roleid"].ToString() != "")
                    {
                        model.Roleid = int.Parse(dt.Rows[n]["Roleid"].ToString());
                    }
                    if (dt.Rows[n]["workid"] != null && dt.Rows[n]["workid"].ToString() != "")
                    {
                        model.workid = dt.Rows[n]["workid"].ToString();
                    }
                    if (dt.Rows[n]["Remark"] != null && dt.Rows[n]["Remark"].ToString() != "")
                    {
                        model.Remark = dt.Rows[n]["Remark"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public  int GetRecordCount(string strWhere)
        {
            return _dUser.GetRecordCount(strWhere);
        }
       


        #endregion  Method
    }
}
